package xyz.bali16.module.netdisk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.netdisk.entity.FileFolder;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderAddParam;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderPageParam;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderUpdateParam;


import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static xyz.bali16.application.core.model.Result.*;

/***
 * 约定: 父目录是自己的id就意味着自己是根目录
 */


@RestController
@RequestMapping(BaseController.API+"filefolder")
public class FileFolderController extends NetdiskBaseController {

    @GetMapping
    @ApiOperation("文件夹列表数据")
    //@PreAuthorize("@auth.permission('plugin:netdisk:filefolder:query')")
    public Result<IPage<FileFolder>> list(FileFolderPageParam param) {
        IPage<FileFolder> fileFolderIPage = fileFolderService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, fileFolderIPage);
    }

    @PostMapping
    @ApiOperation("添加文件夹")
    //@PreAuthorize("@auth.permission('plugin:netdisk:filefolder:add')")
    public Result<String> add(@RequestBody @Valid FileFolderAddParam param) {
        try {
            fileFolderService.add(param);
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "新增文件夹失败", e);
        }
    }

    @PutMapping
    @ApiOperation("修改文件夹")
    //@PreAuthorize("@auth.permission('plugin:netdisk:filefolder:update')")
    public Result<String> update(@RequestBody @Valid FileFolderUpdateParam param) {
        try {
            fileFolderService.update(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.UPDATE_ERROR, "更新标签失败", e);
        }
    }


    @DeleteMapping("/{fileFolderId}")
    @ApiOperation("删除文件目录")
    @ApiImplicitParam(name = "fileFolderId", value = "文件目录id", paramType = "path", required = true)
    //@PreAuthorize("@auth.permission('plugin:netdisk:filefolder:delete')")
    public Result<String> delete(@PathVariable("fileFolderId") String fileFolderId) {
        try {
            fileFolderService.delete(fileFolderId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }
}
